# Contributor: Sergei Lukin <sergej.lukin@gmail.com>
# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Pierre-Gilas MILLON <pgmillon@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=libgit2
pkgver=1.7.1
pkgrel=0
pkgdesc="linkable library for Git"
url="https://libgit2.org/"
arch="all"
license="GPL-2.0-only WITH GCC-exception-2.0"
depends_dev="curl-dev libssh2-dev http-parser-dev"
makedepends_host="$depends_dev zlib-dev openssl-dev>3 pcre2-dev"
makedepends_build="python3 cmake samurai"
subpackages="$pkgname-dbg $pkgname-static $pkgname-dev $pkgname-tools $pkgname-tests::noarch"
options="net"
source="$pkgname-$pkgver.tar.gz::https://github.com/libgit2/libgit2/archive/v$pkgver.tar.gz
	fix-util-tests.patch
	missing-header.patch
	no-online-test.patch
	"

# secfixes:
#   1.4.4-r0:
#     - CVE-2022-29187
#     - CVE-2022-24765
#   0.28.4-r0:
#     - CVE-2019-1348
#     - CVE-2019-1349
#     - CVE-2019-1350
#     - CVE-2019-1351
#     - CVE-2019-1352
#     - CVE-2019-1353
#     - CVE-2019-1354
#     - CVE-2019-1387
#   0.27.4-r0:
#     - CVE-2018-15501
#   0.27.3-r0:
#     - CVE-2018-10887
#     - CVE-2018-10888
#   0.25.1-r0:
#     - CVE-2016-10128
#     - CVE-2016-10129
#     - CVE-2016-10130
#   0.24.3-r0:
#     - CVE-2016-8568
#     - CVE-2016-8569

build() {
	CFLAGS="$CFLAGS -flto=auto" \
	cmake -B build-shared -G Ninja \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DUSE_HTTP_PARSER=system \
		-DUSE_HTTPS=OpenSSL \
		-DREGEX_BACKEND=pcre2 \
		-DUSE_BUNDLED_ZLIB=OFF \
		-DUSE_SSH=ON
	cmake --build build-shared

	cmake -B build-static -G Ninja \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DUSE_HTTP_PARSER=system \
		-DUSE_HTTPS=OpenSSL \
		-DREGEX_BACKEND=pcre2 \
		-DUSE_BUNDLED_ZLIB=OFF \
		-DBUILD_TESTS=OFF \
		-DBUILD_SHARED_LIBS=OFF
	cmake --build build-static
}

check() {
	cd "$builddir"/build-shared
	# Test 'offline' fails on s390x, aarch64 and ppc64le
	ctest -E 'offline|online' --output-on-failure
}

package() {
	DESTDIR="$pkgdir" cmake --install build-static
	DESTDIR="$pkgdir" cmake --install build-shared

	mkdir -p "$pkgdir"/usr/src/$pkgname/
	cp -a tests "$pkgdir"/usr/src/$pkgname/tests
}

tools() {
	pkgdesc="$pkgdesc (tools)"

	amove usr/bin
}

tests() {
	pkgdesc="$pkgdesc (tests)"

	amove usr/src/$pkgname
}

sha512sums="
08e60dde0cdf57006cf0c5f27a82220aba8d701bf7d6d709ffe312975ffa5dbef009ccb1e7cbe570b9502f4361d06ace44dbfd9a5f5f8ad08e9b0ca05bef765e  libgit2-1.7.1.tar.gz
e2409f8e446c5742099787dedae12c5f4933ca95f3841e7ee571b85d6a862645defcfd38bb7b6607ff6e9f25544b9d5e0bb434aad0a7af6a2d22dcf27f2d29c7  fix-util-tests.patch
e116b403d1b8365e86837d15e6864220744e675d1ab06e5c310fd387a791e89a6627df7f45c656786361f6fc74e3d47457258aa0c3069d630b4cb1a2b32ac29f  missing-header.patch
0d4e3aa518a0a114b7b57244c5968c0d43d3ffc90e9b605e72886288a7d42b3a63d9665229e37ba2176d82f29f2eda8d40aba990c9b8688212d2b8c68465e1fc  no-online-test.patch
"
